﻿using SnackXpress.Observer.Subject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SnackXpress.Observer.Observer;
using System.Collections;

namespace SnackXpress.Observer.Subject
{
    public class DiscountSubject : ISubject
    {
        private int discount;

        private List<IObserver> observers;

        public DiscountSubject()
        {
            observers = new List<IObserver>();
        }  

        public void RegisterObserver(IObserver o)
        {
            observers.Add(o);
        }

        public void RemoveObserver(IObserver o)
        {
            int i = observers.IndexOf(o);

            if (i >= 0)
            {
                observers.RemoveAt(i);
            }
        }

        public void NotifyObservers()
        {
            if (observers.Count > 0)
            {
                foreach (IObserver o in observers)
                {
                    o.Update(discount);
                }
            }
        }

        public void setDiscount(bool check, string type)
        {
            if(type.Equals("frikandel"))
            {
                this.discount = (check) ? 30 : 0;
            }
            else if (type.Equals("hamburger"))
            {
                this.discount = (check) ? 15 : 0;
            }

            this.NotifyObservers();
        }
    }
}
